package command

import (
	"fmt"
	"sync"
)

type MotherBoard struct{}

func (*MotherBoard) Start() {
	fmt.Print("system starting\n")
}
func (*MotherBoard) Reboot() {
	fmt.Print("system rebooting\n")
}

type Command interface {
	Execute()
}
type StartCommand struct {
	mb *MotherBoard
}

func NewStartCommand(mb *MotherBoard) *StartCommand {
	return &StartCommand{mb: mb}
}
func (c *StartCommand) Execute() {
	c.mb.Start()
}

type RebootCommand struct {
	mb *MotherBoard
}

func NewRebootCommand(mb *MotherBoard) *RebootCommand {
	return &RebootCommand{mb: mb}
}
func (r *RebootCommand) Execute() {
	r.mb.Reboot()
}

type Box struct {
	butto1 Command
	butto2 Command
}

func NewBox(butto1 Command, butto2 Command) *Box {
	return &Box{butto1: butto1, butto2: butto2}
}
func (b *Box) PressButton1() {
	b.butto1.Execute()
}
func (b *Box) PressButto2() {
	b.butto2.Execute()
}

type Huahua struct {
	bt1          Command
	pressBtl     Command
	exBtl        Command
	fn           []Command
	initIsClosed chan bool
}

func (h *Huahua) isClosed() bool {
	select {
	case <-h.initIsClosed:
		return true
	default:
		return false
	}
}
func NewHuahua(bt1 Command, pressBtl Command, exBtl Command, f ...Command) *Huahua {
	baseci := &Huahua{bt1: bt1, pressBtl: pressBtl, exBtl: exBtl}
	baseci.initIsClosed = make(chan bool)
	if len(f) > 0 {
		baseci.fn = f
	}
	go baseci.Init()
	//初始化是在写成初始化处理
	// wg.add
	// defer wg.done
	// for
	// c.init
	// go func
	// wg.wait
	// isCLose<-true
	// wg
	// close
	// 即可
	//如果需要有意义的信息 和处理
	// var wg sync.wait
	// uint32
	// atolom,ic
	// slect
	// wait
	// for
	// is bren
	// zhuxiehcne case
	// go func
	// go basei.init
	// inewo .wait
	// foe is r
	// clase if
	// bia
	// wgk
	// weiu
	// go func
	// go f
	//
	return baseci
}
func (h *Huahua) Init() *Huahua {
	var wg sync.WaitGroup
	defer wg.Done()
	wg.Add(1)
	if len(h.fn) > 0 {
		for _, c := range h.fn {
			c.Execute()
		}
	}
	go func() {
		fmt.Print("堵塞等待")
		wg.Wait()
		h.initIsClosed <- true
	}()

	return h
}

//我们可以协成组合方式去处理方式
//
func (h *Huahua) Wait() {
	for {
		is := h.isClosed()
		if is {
			break
		}
	}
}
